package com.ctg.itrdc.imageconsole.util;

import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

public class RestClientUtils {

	/**
	 * 
	 * @Description: http请求
	 * @author Piao.Wu 2016年10月28日 下午5:15:56
	 * @param url http请求的url
	 * @param restOperation RestOperation
	 * @return 返回响应结果
	 * String
	 *
	 */
	public static String connect(String url, RestOperation restOperation) {
		ResteasyClient client = new ResteasyClientBuilder().build();
		ResteasyWebTarget target = client.target(url);
		Response response = restOperation.request(target);
		try {
			if (response.getStatus() != 200 && response.getStatus() != 201) {
				throw new RuntimeException("Failed with HTTP error code : " + response.getStatus()
						+ " error message is : " + response.readEntity(String.class));
			}
			return response.readEntity(String.class);
		} finally {
			if (null != response) {
				response.close();
			}
			client.close();
		}
	}

}
